<?php
/**
 * 零起飞-(07FLY-CRM)
 * ==============================================
 * 版权所有 2015-2028   成都零起飞网络，并保留所有权利。
 * 网站地址: http://www.07fly.xyz
 * ----------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ==============================================
 * Author: kfrs <goodkfrs@QQ.com> 574249366
 * Date: 2019-10-3
 */

namespace app\ten\logic;

/**
 * 租户逻辑
 */
class TenTenant extends TenBase
{

    /**
     * 获取权限分组列表
     */
    public function getTenTenantList($where = [], $field = true, $order = 'create_time desc', $paginate = DB_LIST_ROWS)
    {
        $list = $this->modelTenTenant->getList($where, $field, $order, $paginate);
        foreach ($list as &$row) {
            $row['tenant_type_name'] = $this->modelTenTenantType->getValue(['id' => $row['ten_tenant_type_id']], 'name');
            $row['init_admin_name'] = $this->modelTenUser->getValue(['id' => $row['init_admin_id']], 'username');
        }
        return $list;
    }

    /**
     * 权限组添加
     */
    public function tenTenantAdd($data = [])
    {
        $validate_result = $this->validateTenTenant->scene('add')->check($data);
        if (!$validate_result) {
            return [RESULT_ERROR, $this->validateTenTenant->getError()];
        }
        $url = url('show');
        $result = $this->modelTenTenant->setInfo($data);
        $result && action_log('新增', '新增租户：name' . $data['company']);
        return $result ? [RESULT_SUCCESS, '添加成功', $url] : [RESULT_ERROR, $this->modelTenTenant->getError()];
    }

    /**
     * 权限组编辑
     */
    public function tenTenantEdit($data = [])
    {
        $validate_result = $this->validateTenTenant->scene('edit')->check($data);
        if (!$validate_result) {
            return [RESULT_ERROR, $this->validateTenTenant->getError()];
        }
        $url = url('tenTenant');
        $result = $this->modelTenTenant->setInfo($data);
        $result && action_log('编辑', '编辑租户，name：' . $data['company']);
        return $result ? [RESULT_SUCCESS, '编辑成功', $url] : [RESULT_ERROR, $this->modelTenTenant->getError()];
    }

    /**
     * 权限组删除
     */
    public function tenTenantDel($where = [])
    {
        $result = $this->modelTenTenant->deleteInfo($where, true);
        $result && action_log('删除', '删除租户，where：' . http_build_query($where));
        return $result ? [RESULT_SUCCESS, '删除成功'] : [RESULT_ERROR, $this->modelTenTenant->getError()];
    }

    /**
     * 获取权限组信息
     */
    public function getTenTenantInfo($where = [], $field = true)
    {
        $info =  $this->modelTenTenant->getInfo($where, $field);
        if(!empty($info)){
            $info['init_admin_name']=$this->modelTenUser->getValue(['id'=>$info['init_admin_id']],'username');
            $info['ten_tenant_type_name']=$this->modelTenTenantType->getValue(['id'=>$info['ten_tenant_type_id']],'name');

            $info['tenant_user_list']=$this->modelTenUser->getList(['ten_tenant_id'=>$info['id']],'*');

        }
        return $info;
    }

    // 获取搜索条件
    public function getWhere($data = [])
    {
        $where = [];
        if (!empty($data['keywords'])) {
            $where['company|linkman|mobile'] = ['like', '%' . $data['keywords'] . '%'];
        }
        //创建时间
        if (!empty($data['create_time'])) {
            $range_date = rangedate2arr($data['create_time'], "-", 'int');
            $where['create_time'] = ['between', $range_date];
        }
        return $where;
    }

    /**
     * 获取权限组信息
     */
    public function create_user($data)
    {
        $where = [];
        !empty($data['username']) ? $where['username'] = ['=', $data['username']] : '';
        !empty($data['id']) ? $where['org_id'] = ['=', $data['id']] : '';
        $userinfo = $this->logicTenUser->getTenUserInfo($where);
        if ($userinfo) {
            return [RESULT_ERROR, '当前企业的管理员帐号已存在'];
        } else {
            $updata = [
                'username' => $data['username'],
                'password' => data_md5_key($data['password']),
                'mobile' => $data['mobile'],
                'intro' => $data['intro'],
                'org_id' => $data['id']
            ];
            $result = $this->modelTenUser->setInfo($updata);
            //初始化权限
            if ($result) {
                $auth_data = [
                    'sys_user_id' => $result,
                    'sys_auth_id' => 1,//默认为企业会员权限
                    'org_id' => $data['id'],
                ];
                $this->modelSysAuthAccess->setInfo($auth_data);
            }
            $result && action_log('新增', '企业会员系统初始化新增系统用户，name：' . $data['username']);
            return $result ? [RESULT_SUCCESS, '系统用户添加成功'] : [RESULT_ERROR, $this->modelTenUser->getError()];
        }
    }
}